package com.boyuan.system.service.impl;

import com.boyuan.system.manager.HkCameraManager;
import com.boyuan.system.model.vo.LoginVo;
import com.boyuan.system.websocket.BlockQueue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.boyuan.system.utils.PreviewUtil.*;

/**
 * @author ply
 * @date 2023/11/20 - 15:55
 */
@Service
@Slf4j
public class HkCameraService {

    @Resource
    private HkCameraManager hkCameraManager;

    /**
     * 云台控制开始
     * @param loginVo
     * @param command
     * @return
     */
    public Boolean playControlStart(LoginVo loginVo, String command) {
        if (!isInit) {
            init();
        }
        //如果未登录，就先登录
        String userId = BlockQueue.findUserIdByIp(loginVo.getIp());
        List<LoginVo> loginVoList = new ArrayList<>();
        if (userId == null || userId.equals("")) {
            loginVoList.add(loginVo);
            //设备登录
            loginCamera(loginVoList);
        }
        Boolean playControlAsync = hkCameraManager.playControlStartAsync(userId.length() > 0 ? Integer.parseInt(userId) : loginVoList.get(0).getUserId(), 1, command);
        return playControlAsync;
    }

    /**
     * 云台控制结束
     * @param loginVo
     * @param command
     * @return
     * @throws InterruptedException
     */
    public Boolean playControlStop(LoginVo loginVo, String command) {
        if (!isInit) {
            init();
        }
        //如果未登录，就先登录
        String userId = BlockQueue.findUserIdByIp(loginVo.getIp());
        List<LoginVo> loginVoList = new ArrayList<>();
        if (userId == null || userId.equals("")) {
            loginVoList.add(loginVo);
            //设备登录
            loginCamera(loginVoList);
        }
        Boolean playControlAsync = hkCameraManager.playControlStopAsync(userId.length() > 0 ? Integer.parseInt(userId) : loginVoList.get(0).getUserId(), 1, command);
        return playControlAsync;
    }

    /**
     * 实时温度获取
     * @param loginVo
     * @return
     * @throws InterruptedException
     */
    public Map<String,Float> getTemperature(LoginVo loginVo) throws InterruptedException {
        if (!isInit) {
            init();
        }
        //如果未登录，就先登录
        String userId = BlockQueue.findUserIdByIp(loginVo.getIp());
        List<LoginVo> loginVoList = new ArrayList<>();
        if (userId == null || userId.equals("")) {
            loginVoList.add(loginVo);
            //设备登录
            loginCamera(loginVoList);
        }
        hkCameraManager.getTemperature(userId.length() > 0 ? Integer.parseInt(userId) : loginVoList.get(0).getUserId(),loginVo);
        Thread.sleep(500);
        Map<String,Float> temperatureMap = BlockQueue.findTemperatureByIp(loginVo.getIp());
        return temperatureMap;
    }
}
